VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "clsTrayIcon"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

Private Declare Function Shell_NotifyIcon Lib "shell32" _
         Alias "Shell_NotifyIconA" _
         (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean

Private Type NOTIFYICONDATA
        cbSize As Long
        hwnd As Long
        uId As Long
        uFlags As Long
        uCallBackMessage As Long
        hIcon As Long
        szTip As String * 64
End Type

'Declare the constants for the API function. These constants can be
'found in the header file Shellapi.h.

'The following constants are the messages sent to the
'Shell_NotifyIcon function to add, modify, or delete an icon from the
'taskbar status area.
Private Const NIM_ADD = &H0
Private Const NIM_MODIFY = &H1
Private Const NIM_DELETE = &H2

'The following constant is the message sent when a mouse event occurs
'within the rectangular boundaries of the icon in the taskbar status
'area.
Private Const WM_MOUSEMOVE = &H200

'The following constants are the flags that indicate the valid
'members of the NOTIFYICONDATA data type.
Private Const NIF_MESSAGE = &H1
Private Const NIF_ICON = &H2
Private Const NIF_TIP = &H4

'The following constants are used to determine the mouse input on the
'the icon in the taskbar status area.

'Left-click constants.
Private Const WM_LBUTTONDBLCLK = &H203   'Double-click
Private Const WM_LBUTTONDOWN = &H201     'Button down
Private Const WM_LBUTTONUP = &H202       'Button up

'Right-click constants.
Private Const WM_RBUTTONDBLCLK = &H206   'Double-click
Private Const WM_RBUTTONDOWN = &H204     'Button down
Private Const WM_RBUTTONUP = &H205       'Button up

'Middle-click constants
Private Const WM_MBUTTONDBLCLK = &H209
Private Const WM_MBUTTONDOWN = &H207
Private Const WM_MBUTTONUP = &H208
      
Private nid As NOTIFYICONDATA
Public Sub SetTip(pstrTip As String)
    nid.szTip = pstrTip & vbNullChar
    Shell_NotifyIcon NIM_MODIFY, nid
End Sub
Public Sub InitTrayIcon(pfrmForm As Form, strTip As String)
    
    With nid
        .cbSize = Len(nid)
        .hIcon = pfrmForm.Icon
        .hwnd = pfrmForm.hwnd
        .szTip = strTip & vbNullChar
        .uCallBackMessage = WM_MOUSEMOVE
        .uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
        .uId = vbNull
    End With
    Shell_NotifyIcon NIM_ADD, nid
End Sub

Public Sub DeleteTrayIcon()
       
    Shell_NotifyIcon NIM_DELETE, nid
    
End Sub

' Call this function after a MouseMove event on any form. Pass pfrmForm if
' used with multiple forms.
Public Sub Mdl_MouseMove _
    (Button As Integer, _
     Shift As Integer, _
     X As Single, _
     Y As Single, _
     Optional pfrmForm As Form)
     
     If pfrmForm Is Nothing Then Exit Sub
     
     Dim msg As Long
     msg = X / Screen.TwipsPerPixelX
     Select Case msg
        Case WM_LBUTTONDOWN
            'MsgBox "lbd"
        Case WM_LBUTTONUP
            'MsgBox "lbu"
        Case WM_LBUTTONDBLCLK
            pfrmForm.WindowState = vbNormal
            pfrmForm.Visible = True
        Case WM_RBUTTONDOWN
            'MsgBox "rbd"
        Case WM_RBUTTONUP
        
            Dim mnuRBU As Menu
            Set mnuRBU = pfrmForm.mnuTrayIconMenu
            pfrmForm.PopupMenu mnuRBU, vbPopupMenuRightAlign Or _
                vbPopupMenuRightButton
            
        Case WM_RBUTTONDBLCLK
            'MsgBox "rbdc"
        Case WM_MBUTTONDOWN
            'MsgBox "mbd"
        Case WM_MBUTTONUP
            'MsgBox "mbu"
        Case WM_MBUTTONDBLCLK
            'MsgBox "mbdc"
     End Select
End Sub


